package com.you.concurrent.juc.countdownlatch;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {

    static int num = 5;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(num);

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "--------is end------");
                countDownLatch.countDown();
            }).start();
        }
        //模拟一个线程阻塞
        new Thread(() -> {
            try {
                countDownLatch.await();
                System.out.println("--------sub await thread is end------");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        //主线程阻塞
        countDownLatch.await();
        System.out.println("--------main thread is end------");
    }
}
